home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.04 Apr 95 / TreeAppƒ / Eric's C++ Libraries / Interface Classes / CPPMenuBar.cp < prev    next >
Encoding:
Text File  |  1996-04-04  |  9.9 KB  |  377 lines  |  [TEXT/KAHL]

  1. /***************************************************** IMPLEMENTATION
  2.     DATE:    9/20/93
  3.     AUTHOR: Eric R. Rosé
  4.  
  5.     CLASS:  CPPMenuBar
  6.     
  7.     SUPERCLASS: CPPObject
  8.     
  9.         This C++ class manages a Macintosh menu bar
  10.     
  11. ********************************************************************/
  12.  
  13. #pragma once
  14.  
  15. #include <CPPMenuBar.h>
  16. #include <CPPWindowManager.h>
  17. #include <CPPWindow.h>
  18. #include <CPPApplication.h>
  19. #include <ToolboxTools.h>
  20.  
  21. typedef struct {
  22.     short menuID;
  23.     short itemID;
  24.     short commandID;
  25. } MenuBinding;
  26. typedef MenuBinding *BindingPtr;
  27.  
  28. extern    CPPWindowManager    *gWindowManager;
  29. extern    CPPApplication        *gApplication;
  30.  
  31. /*-----------------------------------------------------------------*/
  32. /*------------------------ PUBLIC METHODS -------------------------*/
  33. /*-----------------------------------------------------------------*/
  34.  
  35.     CPPMenuBar::CPPMenuBar (short ResID, short appleID) : CPPList()
  36.     /* create a menu bar based on the passed-in resource template */
  37.     /* and the ID of the apple menu */
  38.     {
  39.         ClearMenuBar();
  40.         this->appleMenu = NULL;
  41.         this->appleMenuID = appleID;
  42.         this->theMenuBar = GetNewMBar (ResID);
  43.         if (this->theMenuBar)
  44.           SetMenuBar (this->theMenuBar);
  45.         DrawMenuBar();
  46.         SetCommandBindings();
  47.     }
  48.  
  49. /*-----------------------------------------------------------------*/
  50.  
  51.     CPPMenuBar::CPPMenuBar (void) : CPPList ()
  52.     /* create a menu bar which has only the apple menu in it */
  53.     {        
  54.         ClearMenuBar();
  55.         this->theMenuBar = GetMenuBar();
  56.         this->appleMenu = NewMenu (1, "\p\024" );
  57.         this->appleMenuID = 1;
  58.         AppendMenu( appleMenu, "\pAbout…");
  59.         AddDAs (appleMenu);
  60.         InsertMenu( appleMenu, 0 );            /* install in menu bar */
  61.         DrawMenuBar ();
  62.         SetCommandBindings();
  63.     }
  64.  
  65. /*-----------------------------------------------------------------*/
  66.  
  67.     CPPMenuBar::~CPPMenuBar (void)
  68.     {
  69.         if (this->appleMenu)
  70.           DisposeMenu(this->appleMenu);
  71.     }
  72.  
  73. /*-----------------------------------------------------------------*/
  74.  
  75.     Boolean    CPPMenuBar::Member (char *className)
  76.     {
  77.         if (strcmp(className, CPPMenuBar::ClassName()) == 0)
  78.           return TRUE;
  79.         else
  80.           return CPPList::Member(className);
  81.     }
  82.  
  83. /*-----------------------------------------------------------------*/
  84.  
  85.     char    *CPPMenuBar::ClassName (void)
  86.     {
  87.         return "CPPMenuBar";
  88.     }
  89.  
  90. /*-----------------------------------------------------------------*/
  91.  
  92.     void    CPPMenuBar::DoMenu (long menuResult)
  93.     {
  94.         short    menuID = HiWord(menuResult);            
  95.         short    menuItem = LoWord(menuResult);        
  96.  
  97.         DoMenu (menuID, menuItem);
  98.     }
  99.  
  100. /*-----------------------------------------------------------------*/
  101.  
  102.     void    CPPMenuBar::SetUpHierMenus (short startID, short endID)
  103.     /* load any hierarchical menus */
  104.     {
  105.         MenuHandle    TempMenu;
  106.         
  107.         for (short i = startID; i <= endID; i++)
  108.           {
  109.               TempMenu = GetMenu (i);
  110.               if (TempMenu)
  111.                 InsertMenu (TempMenu, -1);
  112.           }
  113.     }
  114.  
  115. /*-----------------------------------------------------------------*/
  116.  
  117.     void    CPPMenuBar::DoMenu (short menuID, short theItem)
  118.     /* figure out what command goes with the chosen item */
  119.     /* and send it to the front window or the application */
  120.     {
  121.         long commandID = MenuToCommand (menuID, theItem);
  122.         CPPWindow    *theWindow;
  123.         
  124.         if (commandID)
  125.           {
  126.               // send the command to the front window (if any)
  127.               if (gWindowManager && 
  128.                   (theWindow = gWindowManager->FrontWindowObject()))
  129.                 {
  130.                   if (!theWindow->DoCommand(commandID))
  131.                     gApplication->DoCommand(commandID);
  132.                 }
  133.               else
  134.                 // otherwise, send the command to the application
  135.                 gApplication->DoCommand(commandID);
  136.           }
  137.         else
  138.           {
  139.               if (appleMenu || (menuID == this->appleMenuID))
  140.                 DoAppleMenuItem (this->appleMenuID, theItem);
  141.           }
  142.         HiliteMenu(0);         // turn menu selection off
  143.     }
  144.  
  145. /*-----------------------------------------------------------------*/
  146.  
  147.     MenuHandle    CPPMenuBar::MenuFromID (short ResID)
  148.     /* return the handle of the menu whose ID is ResID */
  149.     {
  150.         return GetMHandle (ResID);
  151.     }
  152.  
  153. /*-----------------------------------------------------------------*/
  154.  
  155.     void    CPPMenuBar::BindCommand (short MenuID, short ItemID, short CommandID)
  156.     /* add an entry in the command list for the specified menu and item */
  157.     {
  158.         BindingPtr    TempBinding = (BindingPtr)NewPtrClear(sizeof(MenuBinding));
  159.         
  160.         if (TempBinding)
  161.           {
  162.               TempBinding->menuID = MenuID;
  163.               TempBinding->itemID = ItemID;
  164.               TempBinding->commandID = CommandID;
  165.               AppendItem((Ptr)TempBinding);
  166.           }
  167.     }
  168.  
  169. /*-----------------------------------------------------------------*/
  170.  
  171.     void    CPPMenuBar::CheckCommand (short CommandID, Boolean doCheck)
  172.     /* put or remove a checkmark from the menu item specified by CommandID */
  173.     {
  174.         MenuHandle    theMenu;
  175.         short    ItemID;
  176.         
  177.         if (CommandToMenu (CommandID, &theMenu, &ItemID))
  178.           CheckItem(theMenu, ItemID, doCheck);
  179.     }
  180.  
  181. /*-----------------------------------------------------------------*/
  182.  
  183.     void    CPPMenuBar::EnableCommand (short CommandID)
  184.     /* enable the menu item specified by CommandID */
  185.     {
  186.         MenuHandle    theMenu;
  187.         short    ItemID;
  188.         
  189.         if (CommandToMenu (CommandID, &theMenu, &ItemID))
  190.           EnableItem(theMenu, ItemID);
  191.     }
  192.  
  193. /*-----------------------------------------------------------------*/
  194.  
  195.     void    CPPMenuBar::DisableCommand (short CommandID)
  196.     /* disable the menu item specified by CommandID */
  197.     {
  198.         MenuHandle    theMenu;
  199.         short    ItemID;
  200.         
  201.         if (CommandToMenu (CommandID, &theMenu, &ItemID))
  202.           DisableItem(theMenu, ItemID);
  203.     }
  204.  
  205. /*-----------------------------------------------------------------*/
  206.  
  207.     void    CPPMenuBar::MarkCommand    (short CommandID, char markChar)
  208.     /* mark the menu item specified by CommandID with markChar */
  209.     {
  210.         MenuHandle    theMenu;
  211.         short    ItemID;
  212.         
  213.         if (CommandToMenu (CommandID, &theMenu, &ItemID))
  214.           SetItemMark(theMenu, ItemID, markChar);
  215.     }
  216.  
  217. /*-----------------------------------------------------------------*/
  218.  
  219.     void    CPPMenuBar::SetCommandText (short CommandID, StringPtr theString)
  220.     /* set the name of the menu item specified by CommandID */
  221.     {
  222.         MenuHandle    theMenu;
  223.         short    ItemID;
  224.         
  225.         if (CommandToMenu (CommandID, &theMenu, &ItemID))
  226.           SetMenuItemText(theMenu, ItemID, theString);
  227.     }
  228.  
  229. /*-----------------------------------------------------------------*/
  230.  
  231.     void    CPPMenuBar::SetCommandIcon (short CommandID, short theIconID)
  232.     /* set the icon for the menu item specified by CommandID */
  233.     {
  234.         MenuHandle    theMenu;
  235.         short    ItemID;
  236.         
  237.         if (CommandToMenu (CommandID, &theMenu, &ItemID))
  238.           SetItemIcon(theMenu, ItemID, theIconID);
  239.     }
  240.  
  241. /*-----------------------------------------------------------------*/
  242.  
  243.     void    CPPMenuBar::SetCommandStyle (short CommandID, Style newStyle)
  244.     /* set the style of the text for the menu item specified by CommandID */
  245.     {
  246.         MenuHandle    theMenu;
  247.         short    ItemID;
  248.         
  249.         if (CommandToMenu (CommandID, &theMenu, &ItemID))
  250.           SetItemStyle(theMenu, ItemID, newStyle);
  251.     }
  252.  
  253. /*-----------------------------------------------------------------*/
  254. /*---------------------- PROTECTED METHODS ------------------------*/
  255. /*-----------------------------------------------------------------*/
  256.     
  257.     void    CPPMenuBar::AddDAs (MenuHandle theMenu)
  258.     /* call this to add the 'apple menu' list to the menu with the */
  259.     /* specified resource ID */
  260.     {
  261.         AppendMenu( theMenu, "\p(-" );    /* dotted line */
  262.         AddResMenu( theMenu, 'DRVR' );    /* Add DA list */
  263.     }
  264.  
  265. /*-----------------------------------------------------------------*/
  266.     
  267.     void    CPPMenuBar::AddDAs (short ResID)
  268.     /* call this to add the 'apple menu' list to the menu with the */
  269.     /* specified resource ID */
  270.     {
  271.         MenuHandle    tempMenu = MenuFromID (ResID);
  272.         
  273.         if (tempMenu)
  274.           {
  275.             AppendMenu( tempMenu, "\p(-" );    /* dotted line */
  276.             AddResMenu( tempMenu, 'DRVR' );    /* Add DA list */
  277.           }
  278.     }
  279.  
  280. /*-----------------------------------------------------------------*/
  281.  
  282.     Boolean    CPPMenuBar::CommandToMenu (short commandID,
  283.                                                MenuHandle *theMenu, 
  284.                                                short *itemID)
  285.     /* convert a command to a handle to its menu and an item in the menu*/
  286.     /* return TRUE if the menu handle exists */
  287.     {
  288.         short    menuID;
  289.         
  290.         if (CommandToMenuAndItem (commandID, &menuID, itemID))
  291.           {
  292.               if ((*theMenu = MenuFromID (menuID)) != NULL)
  293.                 return TRUE;
  294.               else
  295.                 return FALSE;
  296.           }    
  297.     }
  298.     
  299. /*-----------------------------------------------------------------*/
  300.  
  301.     short    CPPMenuBar::MenuToCommand (short menuID, short itemID)
  302.     /* return the command ID associated with the given menu and item */
  303.     {
  304.         BindingPtr    theBinding;
  305.         for (long i = 1; i <= this->numItems; i++)
  306.           {
  307.               theBinding = (BindingPtr)((*this)[i]);
  308.               if (theBinding)
  309.                 {
  310.                     if ((theBinding->menuID == menuID) &&
  311.                         (theBinding->itemID == itemID))
  312.                       return (theBinding->commandID);
  313.                 }
  314.           }
  315.           
  316.         // if we don't find a binding, return 0
  317.         return 0;
  318.     }
  319.  
  320. /*-----------------------------------------------------------------*/
  321.  
  322.     Boolean    CPPMenuBar::CommandToMenuAndItem (short commandID, short *menuID, 
  323.                                                  short *itemID)
  324.     /* return the menu and item ID associated with the given command */
  325.     {
  326.         BindingPtr    theBinding;
  327.                 
  328.         for (long i = 1; i <= this->numItems; i++)
  329.           {
  330.               theBinding = (BindingPtr)((*this)[i]);
  331.               if (theBinding)
  332.                 {
  333.                     if (theBinding->commandID == commandID)
  334.                       {
  335.                           *menuID = theBinding->menuID;
  336.                           *itemID = theBinding->itemID;
  337.                         return TRUE;
  338.                       }
  339.                 }
  340.           }
  341.           
  342.         // if we don't find the command, return FALSE;
  343.         return FALSE;
  344.     }
  345.  
  346. /*-----------------------------------------------------------------*/
  347.  
  348.     void    CPPMenuBar::DoAppleMenuItem (short appleMenuID, short theItem)
  349.     {
  350.         short    dna;
  351.         Str255    daName;
  352.         GrafPtr    savePort;
  353.         MenuHandle    theAppleMenu = GetMHandle (appleMenuID);
  354.         
  355.         if (theAppleMenu)
  356.           {
  357.             GetPort(&savePort);                 // Save the current port
  358.             GetItem(theAppleMenu, theItem, daName);// get DA Name
  359.             dna = OpenDeskAcc(daName);             // Open the DA
  360.             SetPort(savePort);                     // Restore to the saved port
  361.           }
  362.     }
  363.  
  364. /*-----------------------------------------------------------------*/
  365.  
  366.     void    CPPMenuBar::SetCommandBindings (void)
  367.     /* Set all of the application specific bindings for command */
  368.     /* numbers to menu and item numbers */
  369.     /* SUBCLASS SHOULD OVERRIDE */
  370.     {
  371.     
  372.     }
  373.     
  374. /*-----------------------------------------------------------------*/
  375.  
  376.  
  377.